﻿
using Model.Core;
using Models;
using SCADA_DAQ.ZEMT.Server;
using SIE.SCADA.Common.Log;
using SIE.SCADA.SqlSugar;
using SIE.SCADA.ZemtDemo.Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIE.SCADA.ZemtDemo.ViewModel
{
    public class ProdInfoViewModel : QueryBaseViewModel<scada_product_info>
    {
        PgSqlAccess pgsql = new PgSqlAccess();
        

        public ProdInfoViewModel()
        {
            
        }

        public override void Query()
        {
            using (var ctx = pgsql.GetClient())
            {
                Records = new List<scada_product_info>();
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    Records = ormClient.SqlSugarClient.Queryable<scada_product_info>().ToList();
                }
            }
        }



        public  void Query(string repaircode, string sn,string workbin,string matid,string starttime ,string endtime )
        {
            using (var ctx = pgsql.GetClient())
            {
                Records = new List<scada_product_info>();
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    //if (String.IsNullOrEmpty(WorkID))
                    //{
                    //    Records = ormClient.SqlSugarClient.Queryable<scada_product_info>().ToList();
                    //}
                    //else {
                    //    Records = ormClient.SqlSugarClient.Queryable<scada_product_info>().Where(m => m.disidno==WorkID).ToList();
                    //}
                    string sql = " 1=1 ";
                    if (repaircode != "")
                    {
                        sql += " and  repaircode like  " + "'%" + repaircode + "%'";
                    }
                    if (sn != "")
                    {
                        sql += " and  serial_number like  " + "'%" + sn + "%'";
                    }
                    if (workbin != "")
                    {
                        sql += " and  workbin like  " + "'%" + workbin + "%'";
                    }
                    if (matid != "")
                    {
                        sql += " and  matid like  " + "'%" + matid + "%'";
                    }
                    if (starttime !="" && endtime != "") {
                        sql += " and  begintime between   " + " '" + starttime + "' " + " and " + " '" + endtime + "' ";
                    }
                    var temps = ormClient.SqlSugarClient.Queryable<scada_product_info>().Where(sql).ToList();
                    var productStatus= ormClient.SqlSugarClient.Queryable<scada_product_status>().Where("1=1").ToList();
                    foreach (var record in temps) {
                        string state_desc = productStatus.Where(r => r.state == record.state_int).FirstOrDefault().state_desc;
                        record.state_desc= state_desc;
                    }
                    Records = temps;
                }
            }
        }

        internal void PostEquipmentData()
        {
            if (CurItem != null)
            {
                EquipmentDataManager equipmentDataManager = EquipmentDataManager.CreateInstance();
                equipmentDataManager.PostEquipmentData(CurItem.id);
            }
        }

        public override void Delete()
        {
            if (CurItem != null)
            {
                using (var ctx = pgsql.GetClient())
                {
                    if (pgsql.DBClient is SmdcOrmClient ormClient)
                    {
                        ormClient.SqlSugarClient.Deleteable<scada_product_info>().Where(m => m.id == CurItem.id).ExecuteCommand();
                    }
                }
                //base.Delete();
            }
        }

        public override void Update()
        {
            if (CurItem != null)
            {
                using (var ctx = pgsql.GetClient())
                {
                    if (pgsql.DBClient is SmdcOrmClient ormClient)
                    {
                        ormClient.SqlSugarClient.Updateable(CurItem).Where(m => m.id == CurItem.id).ExecuteCommand();
                    }
                }
                //base.Update();
            }
            //if(string.IsNullOrEmpty(CurItem.equipmentid))
            //{
            //    equipManager.Request(CurItem.equipmentid);
            //}
        }

        internal void GetWorkInfo(string WorkInfoID)
        {
            

        }






        public override void Add()
        {
            using (var ctx = pgsql.GetClient())
            {
                try
                {
                    if (pgsql.DBClient is SmdcOrmClient ormClient)
                    {
                        var workInfo = new scada_product_info();
                        ormClient.SqlSugarClient.Insertable<scada_product_info>(workInfo).IgnoreColumns(it => it == "id").ExecuteCommand();
                        Query();
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error(ex.Message);
                    Logger.Error(ex.StackTrace);
                }
                //base.Add();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void UpdateFromMq()
        {

        }

    }
}
